package com.atguigu.gmall.all.controller;


import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Controller
public class ListController {
    @Resource
    private ListFeignClient listFeignClient;

    @GetMapping("/list")
    public String list(SearchParam searchParam, Model model) {
        //回显页面 关键词
        model.addAttribute("searchParam", searchParam);
        //根据关键词等相关条件查询的返回值对象
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //品牌集合
        model.addAttribute("trademarkList", responseVo.getTrademarkList());
        //URLParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //平台属性
        model.addAttribute("attrsList", responseVo.getAttrsList());
        //排序回显
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);
        //结果集
        model.addAttribute("goodsList", responseVo.getGoodsList());
        //当前页
        model.addAttribute("pageNo", responseVo.getPageNo());
        //总页数
        model.addAttribute("totalPages", responseVo.getTotalPages());

        //选择品牌之后回显已选择结果
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam", trademarkParam);

        //选择平台属性之后回显已选择平台属性结果
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);


        return "list/index";
    }
    //选择平台属性之后回显已选择平台属性结果
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //props=23:4G:运行内存
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId", p[0]);
                map.put("attrValue", p[1]);
                map.put("attrName", p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //选择品牌之后回显已选择结果
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            //trademark=2:华为
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //构建页面排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map map = new HashMap();
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            map.put("type", split[0]);
            map.put("sort", split[1]);
        } else {
            map.put("type", 1);
            map.put("sort", "asc");
        }
        return map;
    }

    //URLParam  SpringIOC容器  单例模式  线程调用本方法时，复制一份到本地线程中让线程执行，本源方法不会执行
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {
            sb.append("keyword=").append(keyword);
        }
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("trademark=").append(trademark);
        }
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                sb.append("props=").append(prop);
            }
        }
        return "/list?" + sb.toString();
    }

}
